package com.tc.activiti.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * @author zengqingfa
 * @date 2019/10/22 15:03
 * @description
 * @email zengqingfa_java@163.com
 */
public class JacksonUtil {
    private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    private JacksonUtil() {
    }

    private static ObjectMapper a(String var0) {
        ObjectMapper var1;
        (var1 = new ObjectMapper()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        var1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        SimpleDateFormat var2 = new SimpleDateFormat(var0);
        var1.setDateFormat(var2);
        var1.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return var1;
    }

    public static boolean isEmpty(Object var0) {
        return BeanUtils.isEmpty(var0);
    }

    public static boolean isNotEmpty(Object var0) {
        return !isEmpty(var0);
    }

    public static boolean isJson(String var0) {
        return isJsonObject(var0) || isJsonArray(var0);
    }

    public static boolean isNotJson(String var0) {
        return !isJson(var0);
    }

    public static boolean isJsonObject(String var0) {
        if (StringUtil.isBlank(var0)) {
            return false;
        } else {
            try {
                return a("yyyy-MM-dd HH:mm:ss").readTree(var0).isObject();
            } catch (Exception var1) {
                logger.error(var1.getMessage(), var1);
                return false;
            }
        }
    }

    public static boolean isNotJsonObject(String var0) {
        return !isJsonObject(var0);
    }

    public static boolean isJsonArray(String var0) {
        if (StringUtil.isBlank(var0)) {
            return false;
        } else {
            try {
                return a("yyyy-MM-dd HH:mm:ss").readTree(var0).isArray();
            } catch (Exception var1) {
                logger.error(var1.getMessage(), var1);
                return false;
            }
        }
    }

    public static boolean isNotJsonArray(String var0) {
        return !isJsonArray(var0);
    }

    public static String toJsonString(Object var0) {
        if (BeanUtils.isEmpty(var0)) {
            return null;
        } else {
            try {
                return a("yyyy-MM-dd HH:mm:ss").writeValueAsString(var0);
            } catch (JsonProcessingException var1) {
                logger.error(var1.getMessage(), var1);
                return null;
            }
        }
    }

    public static String getString(String var0, String var1) {
        Object var2;
        Map var3;
        return (var2 = (var3 = (Map) getDTO(var0, Map.class)).get(var1)) instanceof String ? var2.toString() : toJsonString(var3.get(var1));
    }

    public static Object get(String var0, String var1) {
        return ((Map) getDTO(var0, Map.class)).get(var1);
    }

    public static Map<String, Object> toMap(String var0) {
        return (Map) getDTO(var0, Map.class);
    }

    public static <T> T getDTO(String var0, Class<T> var1) {
        if (StringUtil.isBlank(var0)) {
            return null;
        } else {
            try {
                return a("yyyy-MM-dd HH:mm:ss").readValue(var0, var1);
            } catch (IOException var3) {
                logger.error(var3.getMessage(), var3);
                return null;
            }
        }
    }

    public static <T> T getDTO(String var0, Class<T> var1, String var2) {
        if (StringUtil.isBlank(var0)) {
            return null;
        } else {
            try {
                return a(var2).readValue(var0, var1);
            } catch (IOException var3) {
                logger.error(var3.getMessage(), var3);
                return null;
            }
        }
    }

    public static <T> List<T> getDTOList(String var0, Class<T> var1) {
        if (StringUtil.isBlank(var0)) {
            return null;
        } else {
            try {
                ObjectMapper var2;
                CollectionType var4 = (var2 = a("yyyy-MM-dd HH:mm:ss")).getTypeFactory().constructCollectionType(List.class, var1);
                return (List) var2.readValue(var0, var4);
            } catch (IOException var3) {
                logger.error(var3.getMessage(), var3);
                return null;
            }
        }
    }

    public static <T> List<T> getDTOList(String var0, Class<T> var1, String var2) {
        if (StringUtil.isBlank(var0)) {
            return null;
        } else {
            try {
                ObjectMapper var5;
                CollectionType var4 = (var5 = a(var2)).getTypeFactory().constructCollectionType(List.class, var1);
                return (List) var5.readValue(var0, var4);
            } catch (IOException var3) {
                logger.error(var3.getMessage(), var3);
                return null;
            }
        }
    }
}

